home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 361_01 / sar.c < prev    next >
C/C++ Source or Header  |  1990-10-03  |  3KB  |  107 lines

  1.  
  2. /* SAR ---> Search & Replace.  A Streaming SAR ala Unix sed.
  3.  *
  4.  * J.Ekwall  17 May 89
  5.  *
  6.  * Copyrighted to the Public Domain.  Unlimited Distribution Authorized.
  7.  *
  8.  * User Assumes All Risks/Liabilities.
  9.  *
  10.  * Last Update: 05 October 90/EK
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <io.h>
  16. #include <string.h>
  17. #include <stdek.h>
  18.  
  19. char *Documentation[] = {
  20.     "",
  21.     "Usage:",
  22.     "      SAR \"This\" \"That\" ---> Find <This> & Substitute <That>.",
  23.     "      SAR \"This\" ----------> Find <This> & Remove It.",
  24.     "",
  25.     "Wild Cards:",
  26.     "   ? --> Place Holder in <This>.  Any Character Matches.",
  27.     "   ? Cannot Be First Character In <This>.",
  28.     "   ? --> Place Holder in <That>.  Original Character Remains Unchanged.",
  29.     "   $### --> An ASCII Character w/ Decimal Value <3 Digits>.",
  30.     "   $$ ----> A Single Dollar Sign <$>.",
  31.     "",
  32.     "   SAR is a Filter.",
  33.     "",
  34.     NULL};
  35.  
  36. /* Declare ProtoTypes */
  37. void Expand(char *);
  38. void Usage(void);
  39.  
  40. main (int argc, char *argv[])
  41. {
  42.     int i, c, Flag = FALSE;
  43.     unsigned char This[128], That[128], Tbuff[512];
  44.  
  45.  /* Check for Pipe & Passed Parameters */
  46.     if (INFLOW_EXISTS IS FALSE) Usage();
  47.     if ((argc IS 1) || (argc > 3)) Usage();
  48.     strcpy(This,argv[1]);
  49.     if (argc IS 3) strcpy(That,argv[2]); else *That = NULL;
  50.  
  51.  /* Expand $### Entries (If Any) */
  52.     Expand(This); Expand(That);
  53.  
  54.  /* Do Business */
  55.     i = 0; *Tbuff = NULL;
  56.     while ((c = getchar()) != EOF) {
  57.  
  58.     /* Find <This> */
  59.        if ((c IS This[i]) || (This[i] IS '?')) {
  60.            Flag = TRUE; Tbuff[i++] = c; Tbuff[i] = NULL; continue; }
  61.  
  62.     /* No Match.  Now What? */
  63.        if (Flag IS FALSE) { putchar(c); continue; }  /* Nothing in Tbuff */
  64.        if (This[i] IS NULL) {                        /* Hit! */
  65.           for (i = 0; That[i] != NULL; i++) {       /* Do Replacement */
  66.              if (That[i] != '?') { Tbuff[i] = That[i]; continue; }
  67.              if (i < strlen(This)) continue;
  68.              Tbuff[i] = '?';                       /* User Goofed */
  69.           }
  70.           Tbuff[i] = NULL;
  71.        }
  72.  
  73.     /* Stream Out Text & Re-Check Mis-Matched CHR$ */
  74.        printf("%s",Tbuff); Flag = FALSE; i = 0; *Tbuff = NULL;
  75.        if ((c IS This[i]) || (This[i] IS '?')) {
  76.            Flag = TRUE; Tbuff[i++] = c; Tbuff[i] = NULL; continue;
  77.        } else  putchar(c);
  78.     }
  79.  
  80.  /* Send Anything Left-over in Tbuff$ */
  81.     if (Flag IS TRUE) printf("%s",Tbuff);
  82. }
  83.  
  84. void Expand(char *tp1)
  85. {
  86.    int i;
  87.  
  88.    while ((tp1 = strchr(tp1,'$')) != NULL) {    /* Find '$' */
  89.       if (tp1[1] IS '$') {
  90.           strcpy(tp1,&tp1[1]); continue; }      /* Found $$ => Zip 2nd $ */
  91.       for (*tp1 = NULL, i = 1; i < 4 && isdigit(tp1[i]); i++) {    
  92.           *tp1 = 10 * ((BYTE)(*tp1)) + tp1[i] - '0';
  93.       }
  94.       if (*tp1 IS NULL) Usage();
  95.       strcpy(&tp1[1],&tp1[i]); tp1++;           /* Zip '###' Text */
  96.    }
  97. }
  98.  
  99.  
  100. void Usage(void)
  101. {
  102.     char   **dp = Documentation;
  103.  
  104.     for ( ; *dp; dp++) fprintf(stderr,"%s\n", *dp);
  105.     exit(1);
  106. }
  107.